//
//  GSDKUserLoginHistoryView.m
//  GameSDK
//
//  Created by Johnny on 2017/8/2.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKUserLoginHistoryView.h"
#import "GSDKCommonMethod.h"
@interface GSDKUserLoginHistoryView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray<GSDKUserLoginHistoryViewModel*> * viewModels;
@end

@implementation GSDKUserLoginHistoryView
- (void)dealloc {
    DEBUGMSG(@"GSDKUserLoginHistoryView dealloc");
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        
        self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        self.layer.borderWidth = 0.5;
    }
    return self;
}

- (void)reloadView:(NSArray<GSDKUserLoginHistoryViewModel*> *)viewModels {
    self.viewModels = [viewModels mutableCopy];
    [self reloadData];
}

#pragma mark - UITableView DataSourse
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_viewModels count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdetify = @"PopupTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdetify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    GSDKUserLoginHistoryViewModel * viewModel = _viewModels[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.text = viewModel.userName;
    UIImageView *imageView = nil;
    if ([viewModel.userName isEqualToString:_selectedString]) {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 13)];
        imageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/choose.png"];
        cell.accessoryView = imageView;
    } else {
        cell.accessoryView = nil;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectUserBlock) {
        _selectUserBlock((int)(indexPath.row));
    }
}

@end
